<?php
/**
 * @title 486. 预测赢家 - 递归
 * @author start2004
 */

// ini_set("memory_limit", "128M");

class Solution {

    /**
     * @param Integer[] $nums
     * @return Boolean
     */
    function PredictTheWinner($nums) {
        /**
         * @since 2020-09-01 缓存
         */
        $this->dp = [];

        /**
         * @since 2020-09-01 玩家1,玩家2总和
         */
        list($sum1, $sum2) = self::find($nums);

        /**
         * @return
         */
        return $sum1>=$sum2;
    }


    /**
     * @title 返回玩家1最大总和, 玩家2总和
     * @author start2004
     * @since 2020-09-01 Created
     *
     * @param array $nums
     * @return int[]
     */
    public function find($nums = []){
        /**
         * @since 2020-09-01 检查缓存
         */
        $key = implode(",", $nums);
        if(isset($this->dp[$key])){
            return $this->dp[$key];
        } else {}

        /**
         * @since 2020-09-01 只有2个数的时候
         */
        if(count($nums) == 1){
            $sum1 = $nums[0];
            $sum2 = 0;
        } elseif(count($nums) == 2){
            $sum1 = max($nums);
            $sum2 = min($nums);
        } else {
            /**
             * @since 2020-09-01 头尾2种取法
             */
            $nums2 = $nums;
            $shift = array_shift($nums);
            list($s1, $s2) = self::find($nums);

            $pop = array_pop($nums2);
            list($s3, $s4) = self::find($nums2);

            /**
             * @since 2020-09-01 玩家1最大值
             */
            if($shift+$s2 >= $pop+$s4){
                $sum1 = $shift+$s2;
                $sum2 = $s1;
            } else {
                $sum1 = $pop+$s4;
                $sum2 = $s3;
            }
        }

        /**
         * @return
         */
        $this->dp[$key] = [$sum1, $sum2];
        return [$sum1, $sum2];
    }
}



/**
 * @url http://127.0.0.1/leetcode/202009/2020.09.01.php
 */
$datas = [
    [1, 5, 2],
    [1, 5, 233, 7],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->PredictTheWinner($data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". $data[1][$i][0] ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
